<?php
/**
 * Created by PhpStorm.
 * User: Liujianlin
 * Date: 2017/7/8
 * Time: 11:08
 */

namespace app\controllers\marketing;

use app\components\redis\RedisMarketingEmail;
use yii;

/**
 * 用于查看线上的redis使用情况
 * Class TestRedisController
 * @package app\controllers\marketing
 */
class TestRedisController extends \yii\web\Controller
{
    public $enableCsrfValidation = false; //

    protected $module_name;

    protected $site;

    protected $cachePre='ems:marketing_email:';

    protected $redisGroup;

    public function init()
    {
        parent::init();
        $this->site = '';
        $this->module_name = Yii::$app->params['sites']['marketing_email']['module_name'];

        if (!empty($site) && !empty(Yii::$app->params['sites'][$this->module_name]['web_config'][$this->site]['redisGroup'])) {

            $this->redisGroup = Yii::$app->params['sites'][$this->module_name]['web_config'][$this->site]['redisGroup'];
        } elseif (!empty(Yii::$app->params['sites'][$this->module_name]['redisGroup'])) {

            $this->redisGroup = Yii::$app->params['sites'][$this->module_name]['redisGroup'];
        } else {

            $this->redisGroup = 'cache';
        }

    }

    /**
     * 查看所有key
     */
    public function actionKeys()
    {
        $redis = new RedisMarketingEmail();
        $keys = $redis->keys("*");
        echo "<pre>";
        print_r($keys);
        echo "</pre>";
    }

    /**
     * 查看选项
     */
    public function actionItem()
    {
        $method = yii::$app->request->getMethod();
        $cmd = yii::$app->request->$method("cmd");
        $args = yii::$app->request->$method("args");
        if (empty($args) || empty($cmd)) {
            exit("请传入args和cmd");
        }
        $args = explode(",", $args);

        $key = $args[0];
        //$key = $this->cachePre.$this->site.':'.$key;
        if (isset($args[1]) && is_array($args[1])) {
            $value = array_merge([$key], $args[1]);
        } else {
            $value = $args;
            $value[0] = $key;
        }
        $redisGroup = $this->redisGroup;
        $item = Yii::$app->$redisGroup->redis->executeCommand($cmd, $value);

        echo "<pre>";
        print_r($item);
        echo "</pre>";
    }

}